AOMedia AV1 Codec
decoder.h
1/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12#ifndef AOM_AV1_DECODER_DECODER_H_
13#define AOM_AV1_DECODER_DECODER_H_
14
15#include "config/aom_config.h"
16
17#include "aom/aom_codec.h"
18#include "aom_dsp/bitreader.h"
19#include "aom_scale/yv12config.h"
20#include "aom_util/aom_thread.h"
21
22#include "av1/common/av1_common_int.h"
23#include "av1/common/thread_common.h"
24#include "av1/decoder/dthread.h"
25#if CONFIG_ACCOUNTING
26#include "av1/decoder/accounting.h"
27#endif
28#if CONFIG_INSPECTION
29#include "av1/decoder/inspection.h"
30#endif
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
45typedef struct DecoderCodingBlock {
49 DECLARE_ALIGNED(32, MACROBLOCKD, xd);
58 uint8_t *mc_buf[2];
63 tran_low_t *dqcoeff_block[MAX_MB_PLANE];
68 uint16_t cb_offset[MAX_MB_PLANE];
73 eob_info *eob_data[MAX_MB_PLANE];
78 uint16_t txb_offset[MAX_MB_PLANE];
83 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
85
87
88typedef void (*decode_block_visitor_fn_t)(const AV1_COMMON *const cm,
90 aom_reader *const r, const int plane,
91 const int row, const int col,
92 const TX_SIZE tx_size);
93
94typedef void (*predict_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
96 BLOCK_SIZE bsize);
97
98typedef void (*cfl_store_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
99 MACROBLOCKD *const xd);
100
101typedef struct ThreadData {
103
104 // Coding block buffer for the current superblock.
105 // Used only for single-threaded decoding and multi-threaded decoding with
106 // row_mt == 1 cases.
107 // See also: similar buffer in 'AV1Decoder'.
108 CB_BUFFER cb_buffer_base;
109
110 aom_reader *bit_reader;
111
112 // Motion compensation buffer used to get a prediction buffer with extended
113 // borders. One buffer for each of the two possible references.
114 uint8_t *mc_buf[2];
115 // Mask for this block used for compound prediction.
116 uint8_t *seg_mask;
117 // Allocated size of 'mc_buf'.
118 int32_t mc_buf_size;
119 // If true, the pointers in 'mc_buf' were converted from highbd pointers.
120 int mc_buf_use_highbd; // Boolean: whether the byte pointers stored in
121 // mc_buf were converted from highbd pointers.
122
123 CONV_BUF_TYPE *tmp_conv_dst;
124 uint8_t *tmp_obmc_bufs[2];
125
126 decode_block_visitor_fn_t read_coeffs_tx_intra_block_visit;
127 decode_block_visitor_fn_t predict_and_recon_intra_block_visit;
128 decode_block_visitor_fn_t read_coeffs_tx_inter_block_visit;
129 decode_block_visitor_fn_t inverse_tx_inter_block_visit;
130 predict_inter_block_visitor_fn_t predict_inter_block_visit;
131 cfl_store_inter_block_visitor_fn_t cfl_store_inter_block_visit;
132} ThreadData;
133
134typedef struct AV1DecRowMTJobInfo {
135 int tile_row;
136 int tile_col;
137 int mi_row;
138} AV1DecRowMTJobInfo;
139
140typedef struct AV1DecRowMTSyncData {
141#if CONFIG_MULTITHREAD
142 pthread_mutex_t *mutex_;
143 pthread_cond_t *cond_;
144#endif
145 int allocated_sb_rows;
146 int *cur_sb_col;
147 // Denotes the superblock interval at which conditional signalling should
148 // happen. Also denotes the minimum number of extra superblocks of the top row
149 // to be complete to start decoding the current superblock. A value of 1
150 // indicates top-right dependency.
151 int sync_range;
152 // Denotes the additional number of superblocks in the previous row to be
153 // complete to start decoding the current superblock when intraBC tool is
154 // enabled. This additional top-right delay is required to satisfy the
155 // hardware constraints for intraBC tool when row multithreading is enabled.
156 int intrabc_extra_top_right_sb_delay;
157 int mi_rows;
158 int mi_cols;
159 int mi_rows_parse_done;
160 int mi_rows_decode_started;
161 int num_threads_working;
162} AV1DecRowMTSync;
163
164typedef struct AV1DecRowMTInfo {
165 int tile_rows_start;
166 int tile_rows_end;
167 int tile_cols_start;
168 int tile_cols_end;
169 int start_tile;
170 int end_tile;
171 int mi_rows_to_decode;
172
173 // Invariant:
174 // mi_rows_parse_done >= mi_rows_decode_started.
175 // mi_rows_parse_done and mi_rows_decode_started are both initialized to 0.
176 // mi_rows_parse_done is incremented freely. mi_rows_decode_started may only
177 // be incremented to catch up with mi_rows_parse_done but is not allowed to
178 // surpass mi_rows_parse_done.
179 //
180 // When mi_rows_decode_started reaches mi_rows_to_decode, there are no more
181 // decode jobs.
182
183 // Indicates the progress of the bit-stream parsing of superblocks.
184 // Initialized to 0. Incremented by sb_mi_size when parse sb row is done.
185 int mi_rows_parse_done;
186 // Indicates the progress of the decoding of superblocks.
187 // Initialized to 0. Incremented by sb_mi_size when decode sb row is started.
188 int mi_rows_decode_started;
189 // Boolean: Initialized to 0 (false). Set to 1 (true) on error to abort
190 // decoding.
191 int row_mt_exit;
192} AV1DecRowMTInfo;
193
194typedef struct TileDataDec {
195 TileInfo tile_info;
196 aom_reader bit_reader;
197 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
198 AV1DecRowMTSync dec_row_mt_sync;
199} TileDataDec;
200
201typedef struct TileBufferDec {
202 const uint8_t *data;
203 size_t size;
204} TileBufferDec;
205
206typedef struct DataBuffer {
207 const uint8_t *data;
208 size_t size;
209} DataBuffer;
210
211typedef struct EXTERNAL_REFERENCES {
212 YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
213 int num;
214} EXTERNAL_REFERENCES;
215
216typedef struct TileJobsDec {
217 TileBufferDec *tile_buffer;
218 TileDataDec *tile_data;
219} TileJobsDec;
220
221typedef struct AV1DecTileMTData {
222#if CONFIG_MULTITHREAD
223 pthread_mutex_t *job_mutex;
224#endif
225 TileJobsDec *job_queue;
226 int jobs_enqueued;
227 int jobs_dequeued;
228 int alloc_tile_rows;
229 int alloc_tile_cols;
230} AV1DecTileMT;
231
232#if CONFIG_COLLECT_COMPONENT_TIMING
233#include "aom_ports/aom_timer.h"
234// Adjust the following to add new components.
235enum {
236 decode_mbmi_block_time,
237 decode_token_recon_block_intra_time,
238 predict_inter_block_time,
239 decode_reconstruct_tx_inter_time,
240 decode_token_recon_block_inter_time,
241 decode_token_recon_block_time,
242 parse_decode_block_time,
243 decode_tile_time,
244 decode_tiles_time,
245 av1_loop_filter_frame_time,
246 cdef_and_lr_time,
247 av1_decode_tg_tiles_and_wrapup_time,
248 aom_decode_frame_from_obus_time,
249 kTimingComponents,
250} UENUM1BYTE(TIMING_COMPONENT);
251
252static inline char const *get_component_name(int index) {
253 switch (index) {
254 case decode_mbmi_block_time: return "decode_mbmi_block_time";
255 case decode_token_recon_block_intra_time:
256 return "decode_token_recon_block_intra_time";
257 case predict_inter_block_time: return "predict_inter_block_time";
258 case decode_reconstruct_tx_inter_time:
259 return "decode_reconstruct_tx_inter_time";
260 case decode_token_recon_block_inter_time:
261 return "decode_token_recon_block_inter_time";
262 case decode_token_recon_block_time: return "decode_token_recon_block_time";
263 case parse_decode_block_time: return "parse_decode_block_time";
264 case decode_tile_time: return "decode_tile_time";
265 case decode_tiles_time: return "decode_tiles_time";
266 case av1_loop_filter_frame_time: return "av1_loop_filter_frame_time";
267 case cdef_and_lr_time: return "cdef_and_lr_time";
268 case av1_decode_tg_tiles_and_wrapup_time:
269 return "av1_decode_tg_tiles_and_wrapup_time";
270 case aom_decode_frame_from_obus_time:
271 return "aom_decode_frame_from_obus_time";
272
273 default: assert(0);
274 }
275 return "error";
276}
277#endif
278
279typedef struct AV1Decoder {
280 DecoderCodingBlock dcb;
281
282 DECLARE_ALIGNED(32, AV1_COMMON, common);
283
284 AVxWorker lf_worker;
285 AV1LfSync lf_row_sync;
286 AV1LrSync lr_row_sync;
287 AV1LrStruct lr_ctxt;
288 AV1CdefSync cdef_sync;
289 AV1CdefWorkerData *cdef_worker;
290 AVxWorker *tile_workers;
291 int num_workers;
292 DecWorkerData *thread_data;
293 ThreadData td;
294 TileDataDec *tile_data;
295 int allocated_tiles;
296
297 TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
298 AV1DecTileMT tile_mt_info;
299
300 // Each time the decoder is called, we expect to receive a full temporal unit.
301 // This can contain up to one shown frame per spatial layer in the current
302 // operating point (note that some layers may be entirely omitted).
303 // If the 'output_all_layers' option is true, we save all of these shown
304 // frames so that they can be returned to the application. If the
305 // 'output_all_layers' option is false, then we only output one image per
306 // temporal unit.
307 //
308 // Note: The saved buffers are released at the start of the next time the
309 // application calls aom_codec_decode().
310 int output_all_layers;
311 RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
312 size_t num_output_frames; // How many frames are queued up so far?
313
314 // In order to properly support random-access decoding, we need
315 // to behave slightly differently for the very first frame we decode.
316 // So we track whether this is the first frame or not.
317 int decoding_first_frame;
318
319 int allow_lowbitdepth;
320 int max_threads;
321 int inv_tile_order;
322 int need_resync; // wait for key/intra-only frame.
323 int reset_decoder_state;
324
325 int tile_size_bytes;
326 int tile_col_size_bytes;
327 int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding
328#if CONFIG_ACCOUNTING
329 int acct_enabled;
330 Accounting accounting;
331#endif
332 int sequence_header_ready;
333 int sequence_header_changed;
334#if CONFIG_INSPECTION
335 aom_inspect_cb inspect_cb;
336 void *inspect_ctx;
337 int *sb_bits;
338 int sb_bits_alloc_size;
339#endif
340 int operating_point;
341 int current_operating_point;
342 // If nonzero, the maximum frame size (width * height). 0 means unlimited.
343 unsigned int frame_size_limit;
344 int seen_frame_header;
345 // The expected start_tile (tg_start syntax element) of the next tile group.
346 int next_start_tile;
347
348 // State if the camera frame header is already decoded while
349 // large_scale_tile = 1.
350 int camera_frame_header_ready;
351 size_t frame_header_size;
352 DataBuffer obu_size_hdr;
353 int output_frame_width_in_tiles_minus_1;
354 int output_frame_height_in_tiles_minus_1;
355 int tile_count_minus_1;
356 uint32_t coded_tile_data_size;
357 unsigned int ext_tile_debug; // for ext-tile software debug & testing
358
359 // Decoder has 3 modes of operation:
360 // (1) Single-threaded decoding.
361 // (2) Multi-threaded decoding with each tile decoded in parallel.
362 // (3) In addition to (2), each thread decodes 1 superblock row in parallel.
363 // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1)
364 // or (2) depending on 'max_threads'.
365 unsigned int row_mt;
366
367 EXTERNAL_REFERENCES ext_refs;
368 YV12_BUFFER_CONFIG tile_list_outbuf;
369
370 // Coding block buffer for the current frame.
371 // Allocated and used only for multi-threaded decoding with 'row_mt == 0'.
372 // See also: similar buffer in 'ThreadData' struct.
373 CB_BUFFER *cb_buffer_base;
374 // Allocated size of 'cb_buffer_base'. Currently same as the number of
375 // superblocks in the coded frame.
376 int cb_buffer_alloc_size;
377
378 int allocated_row_mt_sync_rows;
379
380#if CONFIG_MULTITHREAD
381 pthread_mutex_t *row_mt_mutex_;
382 pthread_cond_t *row_mt_cond_;
383#endif
384
385 AV1DecRowMTInfo frame_row_mt_info;
386 aom_metadata_array_t *metadata;
387
388 int context_update_tile_id;
389 int skip_loop_filter;
390 int skip_film_grain;
391 int is_annexb;
392 int valid_for_referencing[REF_FRAMES];
393 int is_fwd_kf_present;
394 int is_arf_frame_present;
395 int num_tile_groups;
396 aom_s_frame_info sframe_info;
397
402 SequenceHeader seq_params;
403
407 bool buffer_removal_time_present;
408
412 struct aom_internal_error_info error;
413
417 unsigned int number_temporal_layers;
418
422 unsigned int number_spatial_layers;
423
424#if CONFIG_COLLECT_COMPONENT_TIMING
428 uint64_t component_time[kTimingComponents];
429 struct aom_usec_timer component_timer[kTimingComponents];
433 uint64_t frame_component_time[kTimingComponents];
434#endif
435} AV1Decoder;
436
437// Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
438// code and returns a nonzero value on failure.
439int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
440 const uint8_t **psource);
441
442// Get the frame at a particular index in the output queue
443int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
444 aom_film_grain_t **grain_params);
445
446int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
447
448aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
449 YV12_BUFFER_CONFIG *sd);
450
451aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
452 int use_external_ref,
453 YV12_BUFFER_CONFIG *sd);
454aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
455 YV12_BUFFER_CONFIG *new_frame,
456 YV12_BUFFER_CONFIG *sd);
457
458struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
459
460void av1_decoder_remove(struct AV1Decoder *pbi);
461void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
462
463void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
464
465void av1_dec_free_cb_buf(AV1Decoder *pbi);
466
467static inline void decrease_ref_count(RefCntBuffer *const buf,
468 BufferPool *const pool) {
469 if (buf != NULL) {
470 --buf->ref_count;
471 // Reference counts should never become negative. If this assertion fails,
472 // there is a bug in our reference count management.
473 assert(buf->ref_count >= 0);
474 // A worker may only get a free framebuffer index when calling get_free_fb.
475 // But the raw frame buffer is not set up until we finish decoding header.
476 // So if any error happens during decoding header, frame_bufs[idx] will not
477 // have a valid raw frame buffer.
478 if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
479 pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
480 buf->raw_frame_buffer.data = NULL;
481 buf->raw_frame_buffer.size = 0;
482 buf->raw_frame_buffer.priv = NULL;
483 }
484 }
485}
486
487#define ACCT_STR __func__
488static inline int av1_read_uniform(aom_reader *r, int n) {
489 const int l = get_unsigned_bits(n);
490 const int m = (1 << l) - n;
491 const int v = aom_read_literal(r, l - 1, ACCT_STR);
492 assert(l != 0);
493 if (v < m)
494 return v;
495 else
496 return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
497}
498
499typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
500 aom_reader *r);
501
502void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
503 aom_reader *r, palette_visitor_fn_t visit);
504
505typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
506 int mi_row, int mi_col, aom_reader *r,
507 PARTITION_TYPE partition, BLOCK_SIZE bsize);
508
510
511#if CONFIG_COLLECT_COMPONENT_TIMING
512static inline void start_timing(AV1Decoder *pbi, int component) {
513 aom_usec_timer_start(&pbi->component_timer[component]);
514}
515static inline void end_timing(AV1Decoder *pbi, int component) {
516 aom_usec_timer_mark(&pbi->component_timer[component]);
517 pbi->frame_component_time[component] +=
518 aom_usec_timer_elapsed(&pbi->component_timer[component]);
519}
520
521static inline char const *get_frame_type_enum(int type) {
522 switch (type) {
523 case 0: return "KEY_FRAME";
524 case 1: return "INTER_FRAME";
525 case 2: return "INTRA_ONLY_FRAME";
526 case 3: return "S_FRAME";
527 default: assert(0);
528 }
529 return "error";
530}
531#endif
532
533#ifdef __cplusplus
534} // extern "C"
535#endif
536
537#endif // AOM_AV1_DECODER_DECODER_H_
Describes the codec algorithm interface to applications.
struct aom_metadata_array aom_metadata_array_t
Array of aom_metadata structs for an image.
Definition aom_image.h:200
void(* aom_inspect_cb)(void *decoder, void *ctx)
Definition aomdx.h:55
struct Accounting Accounting
Definition aomdx.h:50
struct aom_s_frame_info aom_s_frame_info
Structure to hold information about S_FRAME.
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
Contains coding block data required by the decoder.
Definition decoder.h:45
eob_info * eob_data[3]
Definition decoder.h:73
uint8_t * mc_buf[2]
Definition decoder.h:58
tran_low_t * dqcoeff_block[3]
Definition decoder.h:63
uint16_t txb_offset[3]
Definition decoder.h:78
int corrupted
Definition decoder.h:53
MACROBLOCKD xd
Definition decoder.h:49
uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]
Definition decoder.h:83
uint16_t cb_offset[3]
Definition decoder.h:68